<?php

class StudentTransfer_MoveinController extends Zend_Controller_Action
{
    private $_teacherSn;

    public function init()
    {
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = APPLICATION_PATH."/modules/student/languages/$locale/data.ini";
        $ini = new Zend_Config_Ini($path);
        $this->_arrayData = $ini->get('array_data')->toArray();
        $this->view->transfer_kind_arr = $this->_arrayData['transfer']['kind'];

        if ($this->getRequest()->getParam('year_semester')){
            $this->view->year_semester = $this->getRequest()->getParam('year_semester');
            $this->view->class_id = $this->getRequest()->getParam('class_id');
        }
        else{
            $this->view->year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
        }
        $this->_teacherSn = Teacher_Model_Teacher::getSnById($this->view->user->username);

    }

    public function indexAction()
    {
        // action body
        //TODO 初始化
        $form = new StudentTransfer_Form_Movein();
        $this->view->form = $form ;
    }

    /**
     *  取出班級
     */
    public function getclassAction()
    {
        $this->getHelper('layout')->disableLayout();
        $this->getHelper('ViewRenderer')->setNorender();
        $year_semester = $this->getRequest()->getParam('year_semester');

        echo $this->view->ClassNameOptions($year_semester);
        //exit;
    }

    /**
     *  取出異動資料
     */
    public function getTransferDataByidAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn');
        $core = new StudentTransfer_Model_Core();
        $res = $core->getTransferDataByid($sn);
        $this->_helper->json($res);
    }

    /**
     * 取得最後一筆學號
     * @return unknown_type
     */
    public function getlastidAction()
    {
        $request = $this->getRequest();
        $arr = explode("-",$request->getParam('year_semester'));
        $year = (int)$arr[0];
        $semester =(int) $arr[1];
        $arr = explode("-", $request->getParam('grade'));
        $grade = (int) $arr[0];
        $class_sn = (int) $arr[1];
        $result = array();

        $q = Doctrine_Query::create()
        ->select('MAX(student_id) AS mm')
        ->from('Student_Model_StudentSemester')
        ->where('year=?',$year)
        ->andWhere('semester=?',$semester)
        ->andWhere('grade=?',$grade);
        $res = $q->fetchOne(null,Doctrine_Core::HYDRATE_ARRAY);
        $result['student_id'] = $res['mm']+1;

        $q = Doctrine_Query::create()
        ->select('MAX(number) AS mm')
        ->from('Student_Model_StudentSemester')
        ->where('year=?',$year)
        ->andWhere('semester=?',$semester)
        ->andWhere('grade=?',$grade)
        ->andWhere('class_sn=?',$class_sn);
        //        echo $q->getSqlQuery();
        //        echo $year.' '.$semester.' '.$grade.' '.$class_sn;
        $res = $q->fetchOne(null,Doctrine_Core::HYDRATE_ARRAY);
        $result['number'] = $res['mm']+1;

        $this->_helper->json($result);
    }

    /**
     *  該學期所有學生
     */
    public function getStudentAction()
    {
        $this->getHelper('layout')->disableLayout();
        $request = $this->getRequest();
        if ($request->getParam('year_semester')) {
            $arr = explode("-",$request->getParam('year_semester'));
            $year = (int)$arr[0];
            $semester =(int) $arr[1];
        }
        else {
            $year = $this->view->Semester['year'];
            $semester = $this->view->Semester['semester'];
        }
        $kind = $request->getParam('kind');
        $q = Doctrine_Query::create()
        ->select('a.*, b.name AS name, b.identification_number AS identification_number, b.sex AS sex,
          c.student_id AS student_id, c.grade AS grade, c.class_sn AS class_sn, c.number AS number')
        ->from('Student_Model_StudentTransfer a ')
        ->leftJoin('a.Student_Model_Student  b')
        ->leftJoin('b.Student_Model_StudentSemester c')
        ->where('a.year=c.year AND a.semester=c.semester')
        ->andWhere('c.year=?', $year)
        ->andWhere('c.semester=?',$semester)
        ->andWhere('a.causation_kind=?',Student_Model_StudentTransfer::MOVE_IN)
        ->orderBy('a.date DESC');
        $res = $q->fetchArray();
        $this->view->data = $res;
    }

    public function moveinSetAction()
    {

        $updateSn = $this->_teacherSn;
        $sdata = $this->getRequest()->getParams();
        $arr = explode("-",$sdata['year_semester']);
        $year = $arr[0];
        $semester = $arr[1];
        $sdata['move_in']['year'] = $year;
        $sdata['move_in']['semester'] = $semester;
        $sdata['move_in']['causation_kind'] = Student_Model_StudentTransfer::MOVE_IN;

      //  $form = $this->_initMoveinForm();
        $form = new StudentTransfer_Form_Movein();
        $message = array();
        if ($form->isValid($sdata)){
            $data = $sdata['move_in'];
            $arr = array();
            //$arr['sn'] = null;
            $arr['sex'] = $data['sex'];
            $arr['identification_number'] = $data['identification_number'];
            $arr['name'] = $data['name'];
            $arr['update_sn']  = $updateSn;
            $student = new Student_Model_Student();
            $student->fromArray($arr);
            $student->save();
            $sn = $student->sn ;

            unset($arr);

            // $arr['sn'] = null;
            $arr['year'] = $year;
            $arr['semester'] = $semester;
            $arr['student_sn'] = $sn;
            $arr['student_id'] = $data['student_id'];
            $arr['update_sn']  = $updateSn;
            $tempArr = explode('-',$sdata['class_id']);
            $arr['grade'] = (int)$tempArr[0];
            $arr['class_sn'] = (int)$tempArr[1];
            $arr['number'] = $data['number'];
            $studentSemester  = new Student_Model_StudentSemester();
            $studentSemester->fromArray($arr);
            $studentSemester->save();

            $data['student_sn'] = $sn;
            //  $data['sn'] = null;

            $data['teacher_sn'] = $updateSn;
            $studentTransfer = new Student_Model_StudentTransfer();
            $studentTransfer->fromArray($data);
            $studentTransfer->save();
            $this->_helper->json(array('sn'=>$studentTransfer->sn));
        }
        else
        $this->_helper->json(array('error'=>$form->getMessages()));

    }

    /**
     * "轉入" 修改
     * @return unknown_type
     */
    public function moveinEditAction()
    {
        $updateSn = $this->_teacherSn;
        $sdata = $this->getRequest()->getParams();

        $arr = explode("-",$sdata['year_semester']);
        $year = $arr[0];
        $semester = $arr[1];
        $sdata['move_in']['year'] = $year;
        $sdata['move_in']['semester'] = $semester;
        $sdata['move_in']['causation_kind'] = Student_Model_StudentTransfer::MOVE_IN;
        $form = new Student_Form_StudentTransfer();
        $form->removeElement('causation_kind');
        $form->setElementsBelongTo('move_in');
        if($form->isValid($sdata)) {

            $studentTransfer = new Student_Model_StudentTransfer;
            $data = $sdata['move_in'];
            $arr = array();
            $arr['approve_date'] = $data['approve_date'];
            $arr['approve_number'] = $data['approve_number'];
            $arr['approve_organ'] = $data['approve_organ'];
            $arr['approve_word'] = $data['approve_word'];
            $arr['causation'] = $data['causation'];
            $arr['county'] = $data['county'];
            $arr['date'] = $data['date'];
            $arr['school_id'] = $data['school_id'];
            $arr['school_name'] = $data['school_name'];
            $arr['teacher_sn'] = $updateSn;
            $studentTransfer->assignIdentifier($sdata['sn']);
            //  print_r($arr);
            $studentTransfer->fromArray($arr);
            $studentTransfer->save();
            //$student_transfer->update($arr,array('sn = ?' => $sdata['sn']));
            $this->_helper->json(array('sn'=>$sdata['sn']));
        }
        else
        $this->_helper->json(array('error'=>$form->getMessages()));
    }



}

